<template>
  <view class="update-certificate">
    <view class="certificate-box">
      <text class="title">
        凭证上传规范样例
      </text>
      <view class="certificate-img__box">
        <up-image class="img" src="https://cdn.uviewui.com/uview/album/1.jpg"/>
        <view class="btn">
          查看样例
        </view>
      </view>
    </view>

    <view class="certificate-box" @tap="handleUpdateCertificateBoxClick">
      <text class="title">
        凭证上传内容
      </text>
      <view class="certificate-img__box">
        <up-image v-if="certificateImgUrl.length" class="img" :src="houseImgUrlPrefix + certificateImgUrl"/>
        <up-icon v-else name="plus" :size="rpx2px(200)"/>
        <view class="btn">
          上传凭证
        </view>
      </view>
    </view>

    <view class="fixed-btn">
      <up-button
          class="btn__update-certificate"
          shape="circle"
          text="确认上传"
          color="linear-gradient(to right, #FF934C, #FC686F)"
          @tap="handleConfirmUpdateCertificateBtnClick"
      />
    </view>
  </view>
</template>
<script setup>
import {ref, inject} from 'vue'
import {getUrlParamsInH5, getUrlParamsInWeixin, rpx2px} from "@/utils/utils";
import {updateCertificateApi, updateLeaseOrderCertufucateApi} from "@/package_kuafu_apartment/pages/api/order";
import {onLoad} from "@dcloudio/uni-app";
import {customNav} from "@/utils/customNav";
const houseImgUrlPrefix = inject('houseImgUrlPrefix')
const certificateImgUrl = ref('')
const orderId = ref('')
const handleConfirmUpdateCertificateBtnClick = () => {
  switch (payName.value) {
    case '房屋订金':
      updateCertificateByReserve()
      break
    case '房屋租金':
      updateCertificateByRent()
      break
  }
}
const updateCertificateByReserve = async () => {
  const params = {
    id: orderId.value,
    imgPath: certificateImgUrl.value
  }
  const res = await updateCertificateApi(params)
  const {code} = res
  if (code === 200) {
    await uni.showToast({
      title: '提交成功',
      duration: 2000,
      success: () => {
        customNav(`index?id=${orderId.value}`)
      }
    })
  }
}
const packageKuaFuPath = inject('packageKuaFuPath')
const updateCertificateByRent = async () => {
  const params = JSON.parse(orderData.value)
  console.log(params,'pp')
  params.forEach(item => {
    item.paymentDocument = certificateImgUrl.value
    item.payType = '2'
    item.orderType = '1'
  })
  const res = await updateLeaseOrderCertufucateApi(params)
  const {code} = res
  if (code === 200) {
    await uni.showToast({
      title: '提交成功',
      duration: 2000,
      success: () => {
        customNav(`${packageKuaFuPath}/my/pages/order-center/index`)
      }
    })
  }
}
const handleUpdateCertificateBoxClick = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ["original", "compressed"],
    sourceType: ["album", "camera"],
    success: async (res) => {
      const {tempFilePaths} = res || {};
      const tempFile = tempFilePaths[0];
      uni.uploadFile({
        url: "/ht-smart-house/uploadFile/upload",
        filePath: tempFile,
        name: "file",
        success: (res) => {
          const {statusCode, data} = res;
          if (statusCode === 200) {
            const parseData = JSON.parse(data);
            certificateImgUrl.value = parseData.data
          }
        },
      });
    },
  });
}
const payName = ref('')
const orderData = ref('')
onLoad(() => {
  /* #ifdef MP-WEIXIN */
  orderId.value = getUrlParamsInWeixin()?.orderId
  payName.value = getUrlParamsInWeixin()?.payName
  orderData.value = getUrlParamsInWeixin()?.orderData
  /* #endif */

  /* #ifndef MP-WEIXIN */
  orderId.value = getUrlParamsInH5()?.orderId
  payName.value = getUrlParamsInH5()?.payName
  /* #endif */
})
</script>
<style scoped lang=less>
@import "./style/update-certificate";
</style>
